//
//  PromotionActivityCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/7.
//

import UIKit
import FSPagerView
class PromotionActivityCell: FSPagerViewCell {
    var bgImgeView: UIImageView!
    var titleLab: UILabel!
    var contentLab: UILabel!
    var timeLab: UILabel!
    var applicationBtn: UIButton!
    var hideBtn: UIButton!
    var shareBtn: UIButton!
    var model: ActivityListModel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initUI()
        self.addLayout()
    }
    func initUI(){
        
        self.bgImgeView = UIImageView(frame: CGRect(x: 30, y: (kScreenHeight - 366)/2, width: kScreenWidth - 60, height: 366))
         self.bgImgeView.isUserInteractionEnabled = true
         self.bgImgeView.image = UIImage(named: "icon_discount_coupon")
         self.contentView.addSubview(self.bgImgeView)
         
         
         self.titleLab = UILabel(frame: CGRect(x: 20, y: 40, width: self.bgImgeView.frame.size.width - 40, height: 20))
         self.titleLab.font = UIFont(name: Theme.PingFangSCRegular, size: 18)
         self.titleLab.textColor = Theme.titleLabColor
         self.titleLab.text = MMLocalizedString("gain", comment: "获得")
         self.titleLab.textAlignment = .center
         self.bgImgeView.addSubview(self.titleLab)
         
         
         self.contentLab = UILabel(frame: CGRect(x: 20, y: self.titleLab.frame.maxY + 40, width: self.bgImgeView.frame.size.width - 40, height: 30))
         self.contentLab.font = UIFont(name: Theme.PingFangSCMedium, size: 21)
         self.contentLab.textColor = Theme.titleLabColor
         self.contentLab.text = "满100减20"
         self.contentLab.textAlignment = .center
         self.bgImgeView.addSubview(self.contentLab)
         
         
         self.timeLab = UILabel(frame: CGRect(x: 20, y: self.contentLab.frame.maxY + 10, width: self.bgImgeView.frame.size.width - 40, height: 30))
         self.timeLab.font = UIFont(name: Theme.PingFangSCRegular, size: 13)
         self.timeLab.textColor = Theme.titleLabColor
         self.timeLab.text = "2023-1-1至2023-12-12"
         self.timeLab.textAlignment = .center
         self.bgImgeView.addSubview(self.timeLab)
         
         
         self.applicationBtn = UIButton(frame: CGRect(x: (kScreenWidth - 30)/2, y:   self.timeLab.frame.maxY + 10, width: 30, height: 22))
         self.applicationBtn.setTitleColor(Theme.titleLabColor, for: .normal)
         self.applicationBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
         self.applicationBtn.setTitle(MMLocalizedString("application", comment: "只适用于商城产品"), for: .normal)
         self.applicationBtn.isUserInteractionEnabled = false
         self.applicationBtn.layer.borderWidth = 1
         self.applicationBtn.layer.cornerRadius = 11
         self.applicationBtn.clipsToBounds = true
         self.applicationBtn.layer.borderColor = Theme.titleLabColor.cgColor
         self.applicationBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
         self.bgImgeView.addSubview(self.applicationBtn)
         
         let ww = XTManageCenter.countBtnWidth(btn: self.applicationBtn) + 32
         self.applicationBtn.snp.makeConstraints { make in
             make.top.equalTo(self.timeLab.snp.bottom).offset(10)
             make.centerX.equalTo(self.bgImgeView.snp.centerX)
             make.width.equalTo(ww)
             make.height.equalTo(22)
         }
    
         
         self.shareBtn = UIButton(frame: CGRect(x: 38, y: self.bgImgeView.frame.size.height - 71, width: self.bgImgeView.frame.size.width - 76, height: 46))
         // fillCode
        
         self.bgImgeView.addSubview(self.shareBtn)
         self.shareBtn.setTitleColor(Theme.titleLabColor, for: .normal)
         self.shareBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 16)
         self.shareBtn.layer.cornerRadius = 8
         self.shareBtn.layer.masksToBounds = true
         self.shareBtn.setTitle(MMLocalizedString("share_points", comment: "首次分享可获得100积分"), for: .normal)
         self.shareBtn.addTarget(self, action: #selector(shareBtnAntion), for: .touchUpInside)
        
        self.addLayout()
   
    }
    
    
    func addLayout(){
        self.bgImgeView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(366)
        }
        
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(40)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(20)
        }
      
        self.contentLab.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom).offset(40)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(30)
        }
        
        
        self.timeLab.snp.makeConstraints { make in
            make.top.equalTo(self.contentLab.snp.bottom).offset(10)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(30)
        }
        
        
        let ww = XTManageCenter.countBtnWidth(btn: self.applicationBtn) + 32
        self.applicationBtn.snp.makeConstraints { make in
            make.top.equalTo(self.timeLab.snp.bottom).offset(10)
            make.centerX.equalTo(self.bgImgeView.snp.centerX)
            make.width.equalTo(ww)
            make.height.equalTo(22)
        }
      
        self.shareBtn.snp.makeConstraints { make in
            make.left.equalTo(38)
            make.right.equalTo(-38)
            make.height.equalTo(46)
            make.bottom.equalTo(-25)
        }
    }
    //渐变色
    func initLayerNor(frme: CGRect) -> UIImage?{
        let tempView = UIView(frame: frme)
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 1, green: 0.93, blue: 0.53, alpha: 1).cgColor, UIColor(red: 1, green: 0.71, blue: 0, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = tempView.bounds
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
        bgLayer1.endPoint = CGPoint(x: 1, y: 1)
        tempView.layer.addSublayer(bgLayer1)
        return tempView.gradient_image
    }
    
    //渐变色
    func initLayerGift(frme: CGRect) -> UIImage?{
        let tempView = UIView(frame: frme)
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor, UIColor(red: 0.93, green: 0.97, blue: 1, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = tempView.bounds
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
        bgLayer1.endPoint = CGPoint(x: 1, y: 1)
        tempView.layer.addSublayer(bgLayer1)
        return tempView.gradient_image
    }
    
    
    func setModel(model: ActivityListModel){
        self.model = model
        //优惠券类型：0商城满减券，1礼品券，2会员积分
        self.timeLab.isHidden = false
        switch model.couponType {
        case 0:
            self.bgImgeView.image = UIImage(named: "icon_discount_coupon")
            self.contentLab.text = model.name ?? ""
            self.applicationBtn.isHidden = false
            self.shareBtn.isHidden = false
            self.shareBtn.setBackgroundImage(self.initLayerNor(frme: shareBtn.frame), for: .normal)
            self.applicationBtn.setTitle(MMLocalizedString("application", comment: "只适用于商城产品"), for: .normal)
        case 1://1礼品券
            self.bgImgeView.image = UIImage(named: "icon_gift_coupon")
            self.contentLab.text = model.name ?? ""
            self.applicationBtn.isHidden = true
            self.shareBtn.setBackgroundImage(self.initLayerGift(frme: shareBtn.frame), for: .normal)
        default://积分
            self.bgImgeView.image = UIImage(named: "icon_integral")
            self.contentLab.text = (model.name ?? "") + MMLocalizedString("points", comment: "积分")
            self.timeLab.isHidden = true
            self.applicationBtn.isHidden = true
            self.shareBtn.isHidden = false
            self.shareBtn.setBackgroundImage(self.initLayerNor(frme: shareBtn.frame), for: .normal)
        }
        //生日好礼
        if model.sendMethod == 3{
            self.titleLab.text = MMLocalizedString("nice_birthday_present", comment: "生日好礼")
            self.bgImgeView.image = UIImage(named: "icon_birthday")
            self.shareBtn.isHidden = true
        }
        self.timeLab.text = model.validEnd ?? ""
        if model.integral == 0{
            self.shareBtn.setTitle(MMLocalizedString("share", comment: "分享"), for: .normal)
        }else{
            self.shareBtn.setTitle(String(format: MMLocalizedString("share_points", comment: "首次分享可获得100积分"), model.integral ?? 0), for: .normal)
        }
        self.updateConstraints()
    }
    
    //MARK: - Antion
    //分享
    @objc func shareBtnAntion(){
        self.model?.shareActivityBlock?()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
